package com.tony.demo.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.tony.demo.R;

public class FragmentActivity1 extends FragmentActivity {
	private String TAG = getClass().getSimpleName();
//	private TextView mTitle;

	private EntityFragment fragmentEntity;
	private AddressFragment fragmentAddress;
	private PhoneFragment fragmentPhone;
	private TabFragment fragmentTab;
	private Button btnOne, btnTwo, btnThree;

	private FragmentManager mCurFragmentManager;
	private FragmentTransaction fragmentTransaction;

	private int mCurTab;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fragment_layout);

		mCurFragmentManager = getSupportFragmentManager();
		fragmentTransaction = mCurFragmentManager.beginTransaction();

		// fragmentEntity = (EntityFragment) getFragmentManager().findFragmentById(R.id.fragment_entity);
		// fragmentAddress = (AddressFragment) getFragmentManager().findFragmentById(R.id.fragment_address);
		// fragmentPhone = (PhoneFragment) getFragmentManager().findFragmentById(R.id.fragment_phone);

		fragmentTab = (TabFragment) mCurFragmentManager.findFragmentById(R.id.fragment_tab);
		btnOne = (Button) fragmentTab.getView().findViewById(R.id.btn_tab_one);
		btnTwo = (Button) fragmentTab.getView().findViewById(R.id.btn_tab_two);
		btnThree = (Button) fragmentTab.getView().findViewById(R.id.btn_tab_three);
		btnOne.setOnClickListener(tabClickListener);
		btnTwo.setOnClickListener(tabClickListener);
		btnThree.setOnClickListener(tabClickListener);

		fragmentEntity = new EntityFragment();
		fragmentAddress = new AddressFragment();
		fragmentPhone = new PhoneFragment();

		switchTab(0);
//		fragmentTransaction.add(R.id.fragment_container, fragmentEntity, makeFragmentName(1000));
////		fragmentTransaction.add(R.id.fragment_container, fragmentEntity);
////		fragmentTransaction.addToBackStack("entity");
		fragmentTransaction.commit();

		Log.d(TAG, "onCreate over");
	}

	@Override
	public void setContentView(int layoutResID) {
		Log.d(TAG, "setContentView");
		// TODO Auto-generated method stub
		super.setContentView(layoutResID);
		Log.d(TAG, "setContentView over");
	}

	private OnClickListener tabClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.btn_tab_one:
					switchTab(0);
					break;
				case R.id.btn_tab_two:
					switchTab(1);
					break;
				case R.id.btn_tab_three:
					switchTab(2);
					break;
			}
		}
	};

	private void switchTab(int index) {
		// mPreTab = mCurTab;
		mCurTab = index;
		switch (index) {
			case 0:
				fragmentTransaction.detach(fragmentPhone);
				fragmentTransaction.detach(fragmentAddress);
				if (mCurFragmentManager.findFragmentByTag(makeFragmentName(1000)) != null) {
					fragmentTransaction.attach(fragmentEntity);
				}
				else {
					fragmentTransaction.add(R.id.fragment_container, fragmentEntity, makeFragmentName(1000));
				}
				// fragmentTransaction.replace(R.id.fragment_container, fragmentEntity);
				// fragmentTransaction.addToBackStack("entity");
//				 fragmentTransaction.commit();
				break;
			case 1:
				fragmentTransaction.detach(fragmentEntity);
				fragmentTransaction.detach(fragmentPhone);
				if (mCurFragmentManager.findFragmentByTag(makeFragmentName(1001)) != null) {
					fragmentTransaction.attach(fragmentAddress);
				}
				else {
					fragmentTransaction.add(R.id.fragment_container, fragmentAddress, makeFragmentName(1001));
				}
				// fragmentTransaction.replace(R.id.fragment_container, fragmentAddress);
				// fragmentTransaction.addToBackStack("address");
//				 fragmentTransaction.commit();
				break;
			case 2:
				fragmentTransaction.detach(fragmentEntity);
				fragmentTransaction.detach(fragmentAddress);
				if (mCurFragmentManager.findFragmentByTag(makeFragmentName(1002)) != null) {
					fragmentTransaction.attach(fragmentPhone);
				}
				else {
					fragmentTransaction.add(R.id.fragment_container, fragmentPhone, makeFragmentName(1002));
				}
				// fragmentTransaction.replace(R.id.fragment_container, fragmentPhone);
				// fragmentTransaction.addToBackStack("phone");
//				  fragmentTransaction.commit();
				break;
		}
	}

	private String makeFragmentName(int viewId) {
        return "android:switcher:" + viewId;
    }

	@Override
	protected void onStart() {
		Log.d(TAG, "onStart");
		// TODO Auto-generated method stub
		super.onStart();
		Log.d(TAG, "onStart over");
	}

	@Override
	protected void onRestart() {
		Log.d(TAG, "onRestart");
		// TODO Auto-generated method stub
		super.onRestart();
		Log.d(TAG, "onRestart over");
	}

	@Override
	protected void onResume() {
		Log.d(TAG, "onResume");
		// TODO Auto-generated method stub
		super.onResume();
		Log.d(TAG, "onResume over");
	}

	@Override
	protected void onPause() {
		Log.d(TAG, "onPause");
		// TODO Auto-generated method stub
		super.onPause();
		Log.d(TAG, "onPause over");
	}

	@Override
	protected void onStop() {
		Log.d(TAG, "onStop");
		// TODO Auto-generated method stub
		super.onStop();
		Log.d(TAG, "onStop over");
	}

	@Override
	protected void onDestroy() {
		Log.d(TAG, "onDestroy");
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.d(TAG, "onDestroy over");
	}

	@Override
	public void recreate() {
		Log.d(TAG, "recreate");
		// TODO Auto-generated method stub
		super.recreate();
		Log.d(TAG, "recreate over");
	}
}